package com.swss.g.assets.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.swss.g.assets.bean.Asset;
import com.swss.g.assets.bean.AstChangeFinAduit;
import com.swss.g.assets.bean.ChangeAsset;
import com.swss.g.assets.dao.ChangeAssetDAO;
import com.swss.g.base.impl.BaseDAOImpl;
import com.swss.g.user.bean.User;
import com.swss.util.DoDate;

public class ChangeAssetDAOImpl extends BaseDAOImpl<ChangeAsset> implements ChangeAssetDAO {
    
	public boolean FirstChangeAuditUpdate(final List<ChangeAsset> ChangeAssetList)
			throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						try {
							HttpServletRequest request = ServletActionContext.getRequest();
							HttpSession sessions = request.getSession();
							User u = (User) sessions.getAttribute("user");
							
							for (ChangeAsset chageAst : ChangeAssetList) {
								ChangeAsset chageasset = (ChangeAsset) session.load(ChangeAsset.class,chageAst.getId());
								chageasset.setAduit(3);
								chageasset.setFstAdt("1");
								chageasset.setFstAdtName(u.getAccperName().trim());
								chageasset.setFstAdtDate(new DoDate().getDate(new Date()));
								session.update(chageasset);
								session.flush();
								session.clear();
							}
							return true;
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}
				});
		return flag;
	}

	public boolean FinanceChangeAuditUpdate( final List<ChangeAsset> ChangeAssetList,final AstChangeFinAduit astChangeFinAduit)
			throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						try {
							HttpServletRequest request = ServletActionContext.getRequest();
							HttpSession sessions = request.getSession();
							User u = (User) sessions.getAttribute("user");
							
							for (ChangeAsset chageAst : ChangeAssetList) {
								ChangeAsset chageasset = (ChangeAsset) session.load(ChangeAsset.class,chageAst.getId());
								chageasset.setAduit(5);
								chageasset.setChageFinVoucher(astChangeFinAduit.getChageFinVoucher().trim());
								chageasset.setChagejfly(astChangeFinAduit.getChagejfly());
								chageasset.setChagefinAccount(astChangeFinAduit.getChagefinAccount());
								chageasset.setChagefinAdvice(astChangeFinAduit.getChagefinAdvice().trim());
								chageasset.setChageFinAdtDate(new DoDate().getDate(new Date()));
								chageasset.setChageFinAdtName(u.getAccperName().trim());
								
								session.update(chageasset);
								session.flush();
								session.clear();
							}
							return true;
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}
				});
		return flag;
	}

	@SuppressWarnings("unchecked")
	public boolean backAstChangeFinAudit(List<ChangeAsset> changeAssetList,
			String returreas) throws Exception {
		// TODO Auto-generated method stub
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession sessions = request.getSession();
			User u = (User) sessions.getAttribute("user");
			
			Date dateStr = new DoDate().getDate(new Date());
			List<ChangeAsset> backChageFinAuditAll  = new ArrayList();
			
			for (ChangeAsset chageAst : changeAssetList) {
				ChangeAsset chageasset = (ChangeAsset) this.getHibernateTemplate().load(ChangeAsset.class, chageAst.getId());
				chageasset.setChageFinVoucher("");  //处置财务凭单
				chageasset.setChagefinAccount("0");  //处置财务账户
				chageasset.setChagejfly("0");  //处置经费来源
				chageasset.setChagefinAdvice(""); //处置财务意见
				chageasset.setChageFinAdtDate(new DoDate().getStrToDate("1990-1-1")); //处置审核日期
				chageasset.setChageFinAdtName("");  //处置审核人
				chageasset.setReturperson(u.getAccperName().trim()); //退回人
				chageasset.setChageReason(returreas); //退回原因
				chageasset.setReturtime(dateStr); //退回时间
				chageasset.setAduit(0); //0财务退回
				
				backChageFinAuditAll.add(chageasset);
			}
			   this.saveOrUpdateAll(backChageFinAuditAll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	public boolean throughAstChangeFinAudit(List<ChangeAsset> changeassetList)
			throws Exception {
		// TODO Auto-generated method stub
         try {
			List<ChangeAsset> throughchageFinAuditAll  = new ArrayList();
			
			for (ChangeAsset chageast : changeassetList) {
				ChangeAsset chageasset = (ChangeAsset) this.getHibernateTemplate().load(ChangeAsset.class, chageast.getId());
				chageasset.setChageFinVoucher("");  //财务凭单
				chageasset.setChagefinAccount("0");  //财务账户
				chageasset.setChagejfly("0"); //经费来源
				chageasset.setChagefinAdvice("");//财务意见
				chageasset.setAduit(3); //3 财务未审状态
				chageasset.setChageFinAdtDate(new DoDate().getStrToDate("1990-1-1")); //审核日期
				chageasset.setChageFinAdtName(""); //审核人
				
				throughchageFinAuditAll.add(chageasset);
			}
			   this.saveOrUpdateAll(throughchageFinAuditAll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}
	@SuppressWarnings("unused")
	public Asset findOneAstByNo(String astNo) throws Exception {
		// TODO Auto-generated method stub
		Asset asset = null;
		try{
			final String hql="FROM Asset WHERE astNo='"+astNo.trim()+"'";
		    asset = (Asset) this.getHibernateTemplate().execute(new HibernateCallback(){
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					// TODO Auto-generated method stub
					Query query = session.createQuery(hql);
					return (Asset)query.uniqueResult();
				}
			});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return asset;
	}

	public boolean LastChangeAuditUpdate(final List<ChangeAsset> ChangeAssetList)
			throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						try {
							for (ChangeAsset chageAst : ChangeAssetList) {
								ChangeAsset chageasset = (ChangeAsset) session.load(ChangeAsset.class,chageAst.getId());
								chageasset.setAduit(7);
								session.update(chageasset);
								session.flush();
								session.clear();
							}
							return true;
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}
				});
		return flag;
	}

	public boolean backChangeAstLastAudit(List<ChangeAsset> changeassetList,
			String returreas) throws Exception {
		// TODO Auto-generated method stub
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession sessions = request.getSession();
			User u = (User) sessions.getAttribute("user");
			
			Date dateStr = new DoDate().getDate(new Date());
			List<ChangeAsset> backFinAuditAll  = new ArrayList<ChangeAsset>();
			
			for (ChangeAsset chageast : changeassetList) {
				ChangeAsset chageasset = (ChangeAsset) this.getHibernateTemplate().load(ChangeAsset.class, chageast.getId());
				
				chageasset.setChageFinVoucher("");//财务凭单
				chageasset.setChagefinAccount("0");  //财务账户
				chageasset.setChagejfly("0");  //经费来源
				chageasset.setChagefinAdvice(""); //财务意见
				chageasset.setChageFinAdtDate(new DoDate().getStrToDate("1990-1-1")); //审核日期
				chageasset.setChageFinAdtName("");  //审核人
				chageasset.setReturperson(u.getAccperName().trim()); //退回人
				chageasset.setRetureas(returreas); //退回原因
				chageasset.setReturtime(dateStr);
				chageasset.setAduit(2); //终审退回
				
				backFinAuditAll.add(chageasset);
				
			}
			   this.saveOrUpdateAll(backFinAuditAll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

}
